package com.efast.dmom.mes.datahandlers.entity;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.WorkCenter;
import com.efast.dmom.mes.dao.entity.WorkCenterDao;
import com.efast.dmom.mes.datahandlers.base.PlantModelDataHandler;
import com.efast.dmom.mes.entity.WorkCenterEntity;
import com.efast.dmom.mes.util.BaseEntityDataUtil;
import com.efast.dmom.mes.util.TimeUtil;
@Component
public class WorkCenterHandler extends PlantModelDataHandler<WorkCenter>{
	
	@Autowired
	private WorkCenterDao workCenterDao;
	
	@Override
	public ResultBean save(WorkCenter obj) {
		ResultBean resultBean = new ResultBean();
		if(obj==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		WorkCenterEntity entity = obj.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
		BaseEntityDataUtil.setBaseEntityData(entity);
		int save = workCenterDao.save(entity);
		if(save>0) {
			resultBean.setSuccess(true);
		}else {
			resultBean.setSuccess(false);
			resultBean.setMessage("insert fail");
		}
		resultBean.setObject(obj);
		return resultBean;
	}
	@Override
	public ResultBean update(WorkCenter obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());

			workCenterDao.update(obj.getInnerData());
		}
		return resultBean;
		
	}
	@Override
	public ResultBean delete(WorkCenter obj) {
		ResultBean resultBean = new ResultBean();
		if (obj.getInnerData() != null) {

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());

			workCenterDao.delete(obj.getInnerData());
		}
		return resultBean;
	}

	public WorkCenter getById(long id) {
		WorkCenter workCenter = new WorkCenter();
		workCenter.setInnerData(workCenterDao.getById(id));
		return workCenter;
	}

	public WorkCenter getByName(String name) {
		WorkCenter workCenter = new WorkCenter();
		workCenter.setInnerData(workCenterDao.getByName(name));
		return workCenter;
	}


	public WorkCenter getByCode(String workshopCode) {
		WorkCenter workCenter = new WorkCenter();
		workCenter.setInnerData(workCenterDao.getByCode(workshopCode));
		return workCenter;
	}
	
	public int queryEquipmentById (Long id) {
		int queryProductionLineById = workCenterDao.queryEquipmentById(id);
		return queryProductionLineById;
	}

	public int queryStationById (Long id) {
		int queryProductionLineById = workCenterDao.queryStationById(id);
		return queryProductionLineById;
	}
	
	public List<WorkCenter> getAllWorkCenter() {
		List<WorkCenterEntity> entityList = workCenterDao.getAllWorkCenter();
		List<WorkCenter> workCenterList = new ArrayList<WorkCenter>();
		if (entityList != null && entityList.size() > 0) {
		for (WorkCenterEntity entityData:entityList) {
			WorkCenter workCenter=new WorkCenter();
			workCenter.setInnerData(entityData);
			workCenterList.add(workCenter);
		}
		return workCenterList;
		}
		return null;
	}
	public List<WorkCenter> getByLineId( long lineId) {
		List<WorkCenterEntity> entityList = workCenterDao.getByLineId(lineId);
		List<WorkCenter> workCenterList = new ArrayList<WorkCenter>();
		if (entityList != null && entityList.size() > 0) {
		for (WorkCenterEntity entityData:entityList) {
			WorkCenter workCenter=new WorkCenter();
			workCenter.setInnerData(entityData);
			workCenterList.add(workCenter);
		}
		return workCenterList;
		}
		return null;
	}
	@Override
	public ResultBean saveBatch(List<WorkCenter> obj) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public ResultBean updateBatch(List<WorkCenter> obj) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public ResultBean deleteBatch(List<WorkCenter> obj) {
		// TODO Auto-generated method stub
		return null;
	}
}
